<?php
declare (strict_types=1);

namespace app\common\listener;

use app\common\model\Area;
use app\common\model\Config;
use app\common\model\OrderProduct;
use app\common\model\Relation;
use app\common\model\User;
use app\common\tool\account\Account;
use app\common\model\Account as AccountModel;
use think\Exception;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;

class ShoperAward
{
    /**
     * 会员在店主区下单支付成功，奖金秒结，所有奖励均为利润的比例
     * 店主区奖励：奖励进入链豆账户
     * @param $data
     */
    public function handle($order)
    {
        try{
            $config = Config::where('group',11)->column('value','name');
            OrderProduct::with(['product','orders'])->where(['order_id'=>$order->id,'cate_1'=>2])->where('profit','>',0)->select()
                ->each(function ($item) use ($config){
                    //经销商奖励：级差制
                    $this->dealer($item->orders->user,$item,$config);

                    //供应商奖励：根据商品设定的对接会员进行级差计算
                    if($item->product->uid){
                        $this->supply($item->product->user,$item,$config);
                    }

                    //代理奖励：根据下单会员所属地给对应的代理
                    $this->agent($item->orders->user,$item,$config);

                    //特殊身份奖励：1%-5%
                    $this->special($item->orders->user,$item);

            });
        }catch (\Exception $e){
            Log::write("listener.ShoperAward:".$e->getMessage());
            throw new Exception("加载失败：".$e->getMessage(), -1);
        }
    }

    private function dealer($user,$op,$config)
    {
        $rate = [
            '3' => $config['spk_dealer_shoper_rate'],
            '4' => $config['spk_dealer_shoper_h_rate'],
            '5' => $config['spk_dealer_leader_rate'],
            '6' => $config['spk_dealer_district_rate'],
            '7' => $config['spk_dealer_city_rate'],
            '8' => $config['spk_dealer_city_rate']
        ];

        $cate = [
            '3' => AccountModel::SPK_DEALER_SHOPER,
            '4' => AccountModel::SPK_DEALER_SHOPER_HIGH,
            '5' => AccountModel::SPK_DEALER_LEADER,
            '6' => AccountModel::SPK_DEALER_DISTRICT,
            '7' => AccountModel::SPK_DEALER_CITY,
            '8' => AccountModel::SPK_DEALER_CITY,
        ];

        $account = 'chain_bean';
        $note = '店主区团队奖励';

        Relation::opZoneBonus($user,$op,$rate,$account,$cate,$note);
    }

    private function supply($user,$op,$config)
    {
        $rate = [
            '3' => $config['spk_supply_shoper_rate'],
            '4' => $config['spk_supply_shoper_rate'],
            '5' => $config['spk_supply_leader_rate'],
            '6' => $config['spk_supply_district_rate'],
            '7' => $config['spk_supply_city_rate'],
            '8' => $config['spk_supply_city_rate']
        ];

        $cate = [
            '3' => AccountModel::SPK_SUPPLY_SHOPER,
            '4' => AccountModel::SPK_SUPPLY_SHOPER,
            '5' => AccountModel::SPK_SUPPLY_LEADER,
            '6' => AccountModel::SPK_SUPPLY_DISTRICT,
            '7' => AccountModel::SPK_SUPPLY_CITY,
            '8' => AccountModel::SPK_SUPPLY_CITY,
        ];

        $account = 'chain_bean';
        $note = '店主区供应商奖励';

        Relation::opZoneBonus($user,$op,$rate,$account,$cate,$note,true);
    }

    private function agent($user,$op,$config)
    {
        if($city_uid = Area::where('code',$user->city)->value('uid')){
            $rate = $config['spk_agent_city_rate'];
            $amount = roundNum($op->profit * $rate,2);
            $account = 'chain_bean';
            $note = '店主区市级代理奖励';
            Account::getInstance()->basics($city_uid, $account, $amount, $note, '+')
                ->setOrder($op->order_id)->createBonus($op->uid, AccountModel::SPK_AGENT_CITY);
        }

        if($district_uid = Area::where('code',$user->district)->value('uid')){
            $rate = $config['spk_agent_district_rate'];
            $amount = roundNum($op->profit * $rate,2);
            $account = 'chain_bean';
            $note = '店主区区级代理奖励';
            Account::getInstance()->basics($district_uid, $account, $amount, $note, '+')
                ->setOrder($op->order_id)->createBonus($op->uid, AccountModel::SPK_AGENT_DISTRICT);
        }
    }

    private function special($user, $op)
    {
        User::where('is_special',1)->field('id,special_rate')->select()
            ->each(function ($item) use ($op, $user){
                $amount = roundNum($op->profit * $item->special_rate,2);
                $account = 'chain_bean';
                $note = '店主区特殊身份奖励';
                Account::getInstance()->basics($item->id, $account, $amount, $note, '+')
                    ->setOrder($op->order_id)->createBonus($user->id, AccountModel::SPK_SPECIAL);
            });
    }
}